﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Controls;
using AForge.Video;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Interop;
using System.Windows.Controls;

namespace Virtix
{
    class VideoPlayer
    {
        private VideoSourcePlayer playerControl;
        private string ipAddress = String.Empty;
        private Canvas canvas;
        private bool isInitialized = false;

        /// <summary>
        /// Initiates the video player
        /// Start() methods needs to be called for it to start working.
        /// </summary>
        /// <param name="canvas">Canvas on which video will be displayed</param>
        /// <param name="ipAddress">ip addrees if device from witch video will be trasnsimted</param>
        public VideoPlayer(Canvas canvas, String ipAddress)
        {
            if (ipAddress != String.Empty)
            {
                this.ipAddress = ipAddress;
                this.canvas = canvas;

                playerControl = new VideoSourcePlayer();
                playerControl.NewFrame += new VideoSourcePlayer.NewFrameHandler(playerControlNewFrame);

                string connectionString = "http://" + this.ipAddress + ":8080/videofeed";
                IVideoSource videoSource = new MJPEGStream(connectionString);
                playerControl.VideoSource = videoSource;
                isInitialized = true;
            }
        }

        /// <summary>
        /// Handle new frame
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="image">Bitmap image passed from transmitter</param>
        private void playerControlNewFrame(object sender, ref Bitmap image)
        {
            if (isInitialized == true)
            {
                Bitmap bitmap = (Bitmap)image.Clone();

                canvas.Dispatcher.Invoke(
                    DispatcherPriority.Render,
                    (SendOrPostCallback)delegate
                    {
                        drawFrame(bitmap);
                    },
                    null);
            }
        }

        /// <summary>
        /// Draw bitmap on canvas passed in constructor
        /// </summary>
        /// <param name="background">Bitmap to draw</param>
        private void drawFrame(Bitmap background)
        {
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                background.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            System.Windows.Media.Brush backgroundBrush = new ImageBrush(bitmapSource);

            canvas.Background = backgroundBrush;
        }

        /// <summary>
        /// Start play video
        /// </summary>
        public void Start()
        {
            if (isInitialized == false && playerControl.IsRunning == false)
            {
                playerControl.Start();
            }
        }

        /// <summary>
        /// Stop play video
        /// </summary>
        public void Stop()
        {
            if (isInitialized == true && playerControl.IsRunning == true)
            {
                playerControl.Stop();
            }
        }
    }
}
